package org.jeecg.modules.basic.material.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.goods.entity.LwgyMaterialGoods;
import org.jeecg.modules.basic.material.entity.LwgyInStock;
import org.jeecg.modules.basic.material.service.ILwgyInStockService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.storages.entity.LwgyStoragesNum;
import org.jeecg.modules.basic.storages.service.ILwgyStoragesNumService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 物料入库表
 * @Author: jeecg-boot
 * @Date:   2021-03-30
 * @Version: V1.0
 */
@Api(tags="物料入库表")
@RestController
@RequestMapping("/material/lwgyInStock")
@Slf4j
public class LwgyInStockController extends JeecgController<LwgyInStock, ILwgyInStockService> {
	@Autowired
	private ILwgyInStockService lwgyInStockService;

	 @Autowired
	 private ILwgyStoragesNumService lwgyStoragesNumService;
	
	/**
	 * 分页列表查询
	 *
	 * @param lwgyInStock
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "物料入库表-分页列表查询")
	@ApiOperation(value="物料入库表-分页列表查询", notes="物料入库表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyInStock lwgyInStock,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyInStock> queryWrapper = QueryGenerator.initQueryWrapper(lwgyInStock, req.getParameterMap());
		Page<LwgyInStock> page = new Page<LwgyInStock>(pageNo, pageSize);
		IPage<LwgyInStock> pageList = lwgyInStockService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param lwgyInStock
	 * @return
	 */
	 //2022
	@AutoLog(value = "物料入库表-添加")
	@ApiOperation(value="物料入库表-添加", notes="物料入库表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyInStock lwgyInStock,LwgyMaterialGoods lwgyMaterialGoods) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss-SSS");
		lwgyInStock.setStockNo("R"+ simpleDateFormat.format(new Date()));
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		System.out.println("你好:"+sysUser);
		lwgyInStock.setState("1");
		lwgyInStock.setIsPrint("0");
		System.out.println("你好吗:"+lwgyInStock.getAmount());
		lwgyInStock.setWriter(sysUser.getUsername());
		lwgyInStock.setBuyer(sysUser.getUsername());
		lwgyInStock.setWriterTime(new Date());
		lwgyInStock.setCheckUser(sysUser.getUsername());
		lwgyInStock.setCheckDatetime(new Date());
		lwgyInStockService.save(lwgyInStock);
		LwgyStoragesNum lwgyStoragesNum=new LwgyStoragesNum();
		List<LwgyStoragesNum> list=lwgyStoragesNumService.queryPageList(lwgyInStock.getGoodsId(),lwgyInStock.getInStockName());
		if(list.size()>=1){
			lwgyStoragesNum = list.get(0);
			BigDecimal a=lwgyInStock.getInStockCounts();
			/*BigDecimal a=new BigDecimal(lwgyInStock.getInStockcounts());*/
			BigDecimal b=lwgyStoragesNum.getNum();
			BigDecimal c=a.add(b);
			lwgyStoragesNum.setNum(c);
			BigDecimal bD1 = lwgyInStock.getPrice();
			BigDecimal bD2 = lwgyStoragesNum.getPrice();
			BigDecimal b1 = bD1.multiply(a);
			BigDecimal b2 = bD2.multiply(b);
			BigDecimal b3 = b1.add(b2);
			BigDecimal b4 = c;
			BigDecimal bg = b3.divide(b4, 2, BigDecimal.ROUND_HALF_UP);
			lwgyStoragesNum.setPrice(bg);
			lwgyStoragesNumService.updateById(lwgyStoragesNum);
		}

		return Result.OK("添加成功！");
	}



	
	/**
	 *  编辑
	 *
	 * @param lwgyInStock
	 * @return
	 */
	@AutoLog(value = "物料入库表-编辑")
	@ApiOperation(value="物料入库表-编辑", notes="物料入库表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyInStock lwgyInStock) {
		lwgyInStockService.updateById(lwgyInStock);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物料入库表-通过id删除")
	@ApiOperation(value="物料入库表-通过id删除", notes="物料入库表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyInStockService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "物料入库表-批量删除")
	@ApiOperation(value="物料入库表-批量删除", notes="物料入库表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyInStockService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物料入库表-通过id查询")
	@ApiOperation(value="物料入库表-通过id查询", notes="物料入库表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyInStock lwgyInStock = lwgyInStockService.getById(id);
		if(lwgyInStock==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyInStock);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyInStock
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyInStock lwgyInStock) {
        return super.exportXls(request, lwgyInStock, LwgyInStock.class, "物料入库表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyInStock.class);
    }

}
